//
//  MainViewController.m
//  Beauty
//
//  Created by Snow on 16/8/26.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "MainViewController.h"
#import "GXMacro.h"
#import "GXCycleView.h"
#import "ModelOfCycleImage.h"
#import "GXNetWorkingParser.h"
#import "BaseModel.h"
#import "NewProductsTableViewCell.h"
#import "PopularProductsTableViewCell.h"
#import "FeaturedCollectionsTableViewCell.h"
#import "DesignerBrandsTableViewCell.h"
#import "Re_DesignTableViewCell.h"
#import "RelatedProductsTableViewCell.h"
#import "MainSecondViewController.h"
#import "UIView+Frame.h"
#import "CycleImageViewController.h"
#import "ModelOfNewProducts.h"
#import "NewProductViewController.h"
#import "DesignOfStoreViewController.h"
#import "Re_DesignViewController.h"
#import "FeaturedViewController.h"
@interface MainViewController ()<UITableViewDelegate, UITableViewDataSource, GXCycleDelegate, NewProductDelegate, PopularProductDeledate, FeaturedDelegateToMain, DesignToMainDelegate, RelateProductDelegate, Re_DesignDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrayOfTable;
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, strong) UIButton *buttonOfCover;
@property (nonatomic, strong) UIButton *buttonOfInspiration;
@property (nonatomic, strong) UIView *viewOfButton;
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //navigationController的设置
    [self naviOption];
    //数据处理
    [self handleData];
    //创建一系列的viewController
    [self createView];
    
    [self createFresh];
}
- (void)createFresh
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleLoadNewData)];
}
- (void)handleLoadNewData {
    //请求服务器更新数据
    _arrayOfTable = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile/index.php?act=index&op=index_1_5" par:nil success:^(id responseObject) {
        NSDictionary *dictionary = [responseObject objectForKey:@"datas"];
        NSArray *array = dictionary[@"banner"];
        NSMutableArray *arrayOfCycle = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            ModelOfCycleImage *model = [[ModelOfCycleImage alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [arrayOfCycle addObject:model.adv_pic];
            [_arrayOfTable addObject:model];
        }
        GXCycleView *header = [[GXCycleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
        header.delegate = self;
        header.arrayImage = arrayOfCycle;
        _tableView.tableHeaderView = header;
        [_tableView reloadData];
        
    } filed:^(NSError *error) {
    }];
    [self.tableView.mj_header endRefreshing];
}
- (void)naviOption
{
//    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.000 green:0.001 blue:0.130 alpha:0.912];
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 30)];
    label.text = @"封面 Cover";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:18];
    [view addSubview:label];
    label.textAlignment = 2;
    self.image = [[UIImageView alloc] initWithFrame:CGRectMake(150, 10, 15, 15)];
    self.image.image = [UIImage imageNamed:@"下拉"];
    [view addSubview:self.image];
    self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTitleView:)];
    [view addGestureRecognizer:self.tap];
    self.navigationItem.titleView = view;
//    //搜索按钮
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search:)];
//    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"box"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(box:)];
}
//封面更换
- (void)tapTitleView:(UITapGestureRecognizer *)tap
{
    self.viewOfButton = [[UIView alloc] initWithFrame:CGRectMake(0, -60, SCREEN_WIDTH, 60)];
    self.buttonOfCover = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonOfInspiration = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonOfCover.backgroundColor = [UIColor whiteColor];
    self.buttonOfInspiration.backgroundColor = [UIColor whiteColor];
    _buttonOfCover.tintColor = [UIColor lightGrayColor];
    _buttonOfInspiration.tintColor = [UIColor blackColor];
    self.buttonOfCover.frame = CGRectMake(0, 0, SCREEN_WIDTH, 30);
    [_buttonOfCover addTarget:self action:@selector(changeToCover:) forControlEvents:UIControlEventTouchUpInside];
    [_buttonOfInspiration addTarget:self action:@selector(changeToInspiration:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewOfButton addSubview:self.buttonOfCover];
    [self.buttonOfCover setTitle:@"封面 Cover" forState:UIControlStateNormal];
    self.buttonOfInspiration.frame = CGRectMake(0, 30.5, SCREEN_WIDTH, 30);
    [self.viewOfButton addSubview:self.buttonOfInspiration];
    [self.buttonOfInspiration setTitle:@"瞬间 Inspiration" forState:UIControlStateNormal];
    [self.view addSubview:self.viewOfButton];
    [UIView animateWithDuration:0.5 animations:^{
        _image.image = [UIImage imageNamed:@"上拉"];
        self.viewOfButton.x_y = 0;
    }];
    [tap removeTarget:self action:@selector(tapTitleView:)];
    [tap addTarget:self action:@selector(backToTitle:)];
}
- (void)backToTitle:(UIGestureRecognizer *)back
{
    [back removeTarget:self action:@selector(backToTitle:)];
    [back addTarget:self action:@selector(tapTitleView:)];
    [UIView animateWithDuration:0.5 animations:^{
        _image.image = [UIImage imageNamed:@"下拉"];
        self.viewOfButton.x_y = -60;
    }];
}
- (void)changeToCover:(UIButton *)button
{
    _buttonOfCover.tintColor = [UIColor lightGrayColor];
    _buttonOfInspiration.tintColor = [UIColor blackColor];
    [UIView animateWithDuration:0.5 animations:^{
        _image.image = [UIImage imageNamed:@"上拉"];
        self.viewOfButton.x_y = -60;
    }];
    [self.tap removeTarget:self action:@selector(backToTitle:)];
    [self.tap addTarget:self action:@selector(tapTitleView:)];

}
- (void)changeToInspiration:(UIButton *)button
{
    _buttonOfCover.tintColor = [UIColor blackColor];
    _buttonOfInspiration.tintColor = [UIColor lightGrayColor];
    [UIView animateWithDuration:0.5 animations:^{
        _image.image = [UIImage imageNamed:@"上拉"];
        self.viewOfButton.x_y = -60;
    }];
    [self.tap removeTarget:self action:@selector(backToTitle:)];
    [self.tap addTarget:self action:@selector(tapTitleView:)];
    MainSecondViewController *second = [[MainSecondViewController alloc] init];
    [self.navigationController pushViewController:second animated:NO];
}
- (void)handleData
{
    _arrayOfTable = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile/index.php?act=index&op=index_1_5" par:nil success:^(id responseObject) {
    NSDictionary *dictionary = [responseObject objectForKey:@"datas"];
        NSArray *array = dictionary[@"banner"];
        NSMutableArray *arrayOfCycle = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            ModelOfCycleImage *model = [[ModelOfCycleImage alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [arrayOfCycle addObject:model.adv_pic];
            [_arrayOfTable addObject:model];
        }
        GXCycleView *header = [[GXCycleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
        header.delegate = self;
        header.arrayImage = arrayOfCycle;
        _tableView.tableHeaderView = header;

        [_tableView reloadData];

} filed:^(NSError *error) {
}];
}
- (void)createView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[NewProductsTableViewCell class] forCellReuseIdentifier:@"NewProductsTableViewCell"];
    [_tableView registerClass:[PopularProductsTableViewCell class] forCellReuseIdentifier:@"PopularProductsTableViewCell"];
    [_tableView registerClass:[FeaturedCollectionsTableViewCell class] forCellReuseIdentifier:@"FeaturedCollectionsTableViewCell"];
    [_tableView registerClass:[DesignerBrandsTableViewCell class] forCellReuseIdentifier:@"DesignerBrandsTableViewCell"];
    [_tableView registerClass:[Re_DesignTableViewCell class] forCellReuseIdentifier:@"Re_DesignTableViewCell"];
    [_tableView registerClass:[RelatedProductsTableViewCell class] forCellReuseIdentifier:@"RelatedProductsTableViewCell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewProductsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewProductsTableViewCell"];
    cell.delegate = self;
    if (indexPath.section == 1) {
        PopularProductsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PopularProductsTableViewCell"];
        cell.delegate = self;
        return cell;
    } else if(indexPath.section == 2) {
        FeaturedCollectionsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FeaturedCollectionsTableViewCell"];
        cell.delegate = self;
        return cell;
    } else if(indexPath.section == 3) {
        DesignerBrandsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DesignerBrandsTableViewCell"];
        cell.delegate = self;
        return cell;
    } else if (indexPath.section == 4) {
        Re_DesignTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Re_DesignTableViewCell"];
        cell.delegate = self;
        return cell;
    } else if (indexPath.section == 5) {
        RelatedProductsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RelatedProductsTableViewCell"];
        cell.delegate = self;
        return cell;
    }
      return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2 || indexPath.section == 3) {
        return 300;
    } else {
        return 220;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *viewOfHeader = [[UIView alloc]init];
    viewOfHeader.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100);
    UIImageView *image = [[UIImageView alloc] initWithFrame:viewOfHeader.frame];
    [viewOfHeader addSubview:image];
    image.image = [UIImage imageNamed:[NSString stringWithFormat:@"Header_%ld", section + 1]];
    return viewOfHeader;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 100;
}
#pragma mark ------------  去掉tableView的头部分区悬浮效果  -----------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 100;
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, 0, 0);
    }else if (offsetY >= sectionHeaderHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
//轮播图点击
- (void)backOneNsstring:(NSIndexPath *)indexPath
{
    ModelOfCycleImage *model = [_arrayOfTable objectAtIndex:indexPath.row - 1];
    DesignOfStoreViewController *design = [[DesignOfStoreViewController alloc]init];
    design.store_id = model.store_id;
    [self.navigationController pushViewController:design animated:YES];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}
//newproduct点击
- (void)newProductWithNSString:(NSString *)good_id
{
    NewProductViewController *new = [[NewProductViewController alloc] init];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    new.goods_id = good_id;
    [self.navigationController pushViewController:new animated:YES];
}
//popular 点击方法
- (void)popularWithGoodId:(NSString *)good_id
{
    NewProductViewController *new = [[NewProductViewController alloc] init];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    new.goods_id = good_id;
    [self.navigationController pushViewController:new animated:YES];
}
//featured-button点击协议方法
- (void)featuredToMainWithGoodId:(NSString *)good_id
{
    NewProductViewController *new = [[NewProductViewController alloc] init];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    new.goods_id = good_id;
    [self.navigationController pushViewController:new animated:YES];
}
//designbrand - button - delegate
- (void)designToMainWithGoodId:(NSString *)good_id
{
    NewProductViewController *new = [[NewProductViewController alloc] init];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    new.goods_id = good_id;
    [self.navigationController pushViewController:new animated:YES];
}
// design - brands - collection
- (void)designDetailToMianWithStoreID:(NSString *)store_id
{
    DesignOfStoreViewController *design = [[DesignOfStoreViewController alloc]init];
    design.store_id = store_id;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController pushViewController:design animated:YES];
}

//related - button - delegate
- (void)relateWithGoodId:(NSString *)good_id
{
    NewProductViewController *new = [[NewProductViewController alloc] init];
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    new.goods_id = good_id;
    [self.navigationController pushViewController:new animated:YES];
}
//re-design
- (void)reDesignWithUrl:(NSString *)url
{
    Re_DesignViewController *re = [[Re_DesignViewController alloc]init];
    self.tabBarController.tabBar.hidden = YES;
    re.url = url;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    [self.navigationController pushViewController:re animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor =  [UIColor colorWithRed:0.110 green:0.102 blue:0.196 alpha:1.000];
    self.tabBarController.tabBar.hidden = NO;
}
- (void)featuredToMainClickOnImageWithID:(NSString *)special_id
{
    self.tabBarController.tabBar.hidden = YES;
    FeaturedViewController *fea = [[FeaturedViewController alloc]init];
    fea.special_id = special_id;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController pushViewController:fea animated:YES];
}
@end
